package com.cysion.study.ui.fragment

import android.content.Intent
import android.text.TextUtils
import android.view.View
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.cysion.other.addTo
import com.cysion.other.startActivity_ex
import com.cysion.study.R
import com.cysion.study.helper.UserCache
import com.cysion.study.ktbox.Box
import com.cysion.study.ktbox.base.BaseFragment
import com.cysion.study.ktbox.net.BaseResponseRx
import com.cysion.study.ktbox.utils.ACache
import com.cysion.study.ktbox.utils.CLog
import com.cysion.study.net.ApiClient
import com.cysion.study.ui.login.LoginActivity
import com.cysion.study.ui.login.UserSwitchActivity
import com.cysion.study.ui.personal.*
import com.cysion.study.widget.ShowToast
import com.cysion.study.widget.UIHelper
import com.cysion.targetfun._subscribe
import io.reactivex.disposables.CompositeDisposable
import kotlinx.android.synthetic.main.fragment_me.*

/**
 * Created by chu on 2019-06-30.
 */
class MeFragment:BaseFragment(), View.OnClickListener {

    val compositeDisposable: CompositeDisposable = CompositeDisposable()
    override fun getLayoutId(): Int = R.layout.fragment_me

    override fun initView() {
        cv_head.setOnClickListener(this)
        tv_userName.setOnClickListener(this)
        tv_teacher.setOnClickListener(this)
        tv_attpe.setOnClickListener(this)
        tv_apply.setOnClickListener(this)
        tv_money.setOnClickListener(this)
        tv_setting.setOnClickListener(this)
        tv_activity.setOnClickListener(this)
        tv_tui.setOnClickListener(this)
        tv_course.setOnClickListener(this)
        tv_tuo.setOnClickListener(this)
        tv_study.setOnClickListener(this)
        tv_leve.setOnClickListener(this)
    }

    override fun onResume() {
        super.onResume()
        CLog.i("onResume")
        if (!TextUtils.isEmpty(UserCache.token)) {
            updateUserInfo()
        }
    }

    override fun onClick(v: View?) {
        if (TextUtils.isEmpty(UserCache.userId)) {
            context.startActivity_ex<LoginActivity>()
            return
        }
        when (v?.id) {
            R.id.cv_head->{//个人信息
                context.startActivity_ex<PersonalInfoActivity>()
            }
            R.id.tv_userName->{
                context.startActivity_ex<PersonalInfoActivity>()
            }
            R.id.tv_teacher->{
//                ShowToast("老师")
//                UserCache.clear()
                UIHelper.jump(activity, UserSwitchActivity::class.java)
                context.finish()
            }
            R.id.tv_attpe->{//我的审批
                ShowToast("正在开发中,请稍后使用")
//                context.startActivity_ex<MyApprovalActivity>()
            }
            R.id.tv_apply->{//我的申请
                ShowToast("正在开发中,请稍后使用")
//                context.startActivity_ex<ApprovalActivity>()
            }
            R.id.tv_money->{//推广收益
                context.startActivity_ex<ProMoneyActivity>()
            }
            R.id.tv_setting->{//设置
                context.startActivity_ex<SettingActivity>()
            }
            R.id.tv_activity->{//活动管理
//                context.startActivity_ex<ActivityManagesActivity>()
                UIHelper.jump(context,ActivityManagesActivity::class.java)
            }
            R.id.tv_tui->{//课程推广
                context.startActivity_ex<ClassPushManageActivity>()
            }
            R.id.tv_course->{//课程管理
                context.startActivity_ex<ClassPushManageActivity>()
            }
            R.id.tv_study->{//研学
                UIHelper.jump(context, StudiesActivity::class.java)
            }
            R.id.tv_tuo->{//托管
//                context.startActivity_ex<TrusteeshipActivity>()//旧页面 托管  暂停使用
                UIHelper.jump(context, HostingActivity::class.java)
            }
            R.id.tv_leve->{//考级
                ShowToast("正在开发中,请稍后使用")
//                UIHelper.jump(context, LeveManageActivity::class.java)
            }

        }

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        println("####个人中心返回="+requestCode+resultCode)
    }

    override fun lazyLoad() {
        super.lazyLoad()
        if (TextUtils.isEmpty(UserCache.token)) {
            CLog.i("lazyLoad")
            context.startActivity_ex<LoginActivity>()
            return
        }

        getUserInfos()

    }

    private fun getUserInfos() {
        ApiClient.api.getUserInfos(UserCache.token).compose(BaseResponseRx.validateToMain())
            ._subscribe {
                _onNext {
                    if (!TextUtils.isEmpty(it.id)) {
                        UserCache.save(it)
                        updateUserInfo()
                    }
                }
            }.addTo(compositeDisposable)
    }

    private fun updateUserInfo() {

        if (TextUtils.isEmpty(UserCache.userInfo?.nickName ?: "")) {
            getUserInfos()
        }
        /*Glide.with(context).load(UserCache.userInfo?.headImageUrl)
            .apply(RequestOptions.circleCropTransform())
            .into(cv_head)*/

        Glide.with(context).load(UserCache.userInfo?.headImageUrl).apply(
            RequestOptions().placeholder(R.mipmap.default_head)
        ).into(cv_head)

        /*Glide.with(context).load(UserCache.userInfo?.backImageUrl).apply(
            RequestOptions().placeholder(R.mipmap.user_bg)
        ).into(iv_bg)*/

        tv_userName.text = UserCache.userInfo?.nickName ?: "未登录"
        var ttype= ACache.get(Box.context).getAsString("teacherType")
        println("####账号类型普教="+ttype)
        if (ttype == "1") {//普通教师首页fragment
            tv_teacher.text = "普通教师"
        } else {
            tv_teacher.text = "私教"
        }
    }

    override fun visibleAgain() {
        super.visibleAgain()
        CLog.i("visibleAgain中的token="+UserCache.token)
        if (TextUtils.isEmpty(UserCache.token)) {
            CLog.i("visibleAgain")
            UIHelper.jump(activity,LoginActivity::class.java)
            return
        }
        updateUserInfo()
    }

    override fun closeMvp() {
        compositeDisposable.dispose()
    }
}